package linkedList;

import java.util.Iterator;
import java.util.function.Consumer;

//单向链表(带哨兵)
public class SinglyLinkedList2 implements Iterable<Integer>{
    //邵兵节点
    private Node head = new Node(666,null);
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;
            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                int v = p.value;
                p = p.next;
                return v;
            }
        };
    }

    private static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }
//    private Node head;

    //向头部插入节点
    public void addFirst(int value) {
//        //链表为空
//        if (head == null) {
//            head = new Node(value, null);
//        } else {
//            //链表不为空
//            head = new Node(value,head);
//        }
        insert(0,value);

    }

    //遍历链表
    public void loop1() {
        Node p = head.next;
        while(p != null) {
            System.out.println(p.value);
            p = p.next;
        }
    }
    public void loop2() {
        for (Node p = head.next; p != null; p = p.next) {
            System.out.println(p.value);
        }
    }
    //用迭代器迭代
    public void loop3(Consumer<Integer> consumer) {
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    //找到最后一个节点
    private Node findLast() {
//        if (head == null) {
//            return null;
//        }
        Node p;
        for (p = head;p.next != null; p = p.next) {

        }
        return p;
    }

    //向链表的头插入节点
    public void addLast(int value) {
        Node last = findLast();
//        if (last == null) {
//            addFirst(value);
//            return;
//        }
        last.next = new Node(value,null);
    }

    //根据索引找节点
    private Node findNode(int index) {
        int i = -1;
        for (Node p = head.next; p != null; p = p.next,i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    //根据索引返回对应节点的值
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException(
                    String.format("index [%d] 不合法%n",index)
            );
        }
        return node.value;
    }

    //像任意索引位置插入节点
    public void insert(int index, int value) {
        //考虑 index 为 0 的情况 ， 可以之间调用 addFirst()
        if (index == 0) {
            addFirst(value);
        }
        Node prev = findNode(index-1);
        if (prev == null) {
            throw new IllegalArgumentException(
                    String.format("index [%d] 不合法%n",index)
            );
        }
        prev.next = new Node(value,prev.next);
    }

    //删除第一个节点
    public void removeFirst() {
        if (head == null) {
            throw new IllegalArgumentException(
                    String.format("index [%d] 不合法%n",0)
            );
        }
        head = head.next;
    }
    //

    //根据索引删除节点
    public void remove(int index) {
//        //索引为0
//        if (index == 0) {
//            //执行删除第一个节点的fangfa
//            removeFirst();
//            return;
//        }
        //没有找到节点目标结点的前一个节点
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException(
                    String.format("index [%d] 不合法%n",index)
            );
        }
        Node removed = prev.next;
        //目标节点为空
        if (removed == null) {
            throw new IllegalArgumentException(
                    String.format("index [%d] 不合法%n",null)
            );
        }
        prev.next = removed.next;
    }
}
